﻿#include "ExportRecordOfCharge.h"
#include "ui_exportrecordofcharge.h"

ExportRecordOfCharge::ExportRecordOfCharge(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ExportRecordOfCharge)
{
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    accountModel = new QSqlQueryModel(this);
    accountMapper = new QDataWidgetMapper(this);
    accountMapper->setModel(accountModel);
    accountMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);


    QRegExp dateTimeRegExp("(([1-9])\\d)-((0[1-9])|(1[0-2]))\\-((0[1-9])|([1-2]\\d)|(3[0-1]))");
    QValidator* validatorDateTime = new QRegExpValidator(dateTimeRegExp, 0);
    ui->m_editChargeRecordExport_StartTime->setValidator(validatorDateTime);
    ui->m_editChargeRecordExport_EndTime->setValidator(validatorDateTime);

    keyBoardConnect();
}

void ExportRecordOfCharge::on_Keyboard_clicked()
{
    QPushButton* btn =  static_cast<QPushButton*>(sender());

    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();

    keyHandle(btn, focus);

}

void ExportRecordOfCharge::keyHandle(QPushButton* btn, QLineEdit* lineEdit)
{
    if( btn->text() == tr("空格") )
    {
        lineEdit->insert(" ");
    }
    else if( btn->text() == tr("退格") )
    {
        lineEdit->backspace();
    }
    else if( btn->text() == tr("清除"))
    {
        lineEdit->clear();
    }
    else
    {
        lineEdit->insert(btn->text());
    }
}

void ExportRecordOfCharge::keyBoardConnect()
{
    connect(ui->One, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Tow, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Three, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Four, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Five, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Six, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Seven, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Eight, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Nine, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Zero, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Remove, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->RemoveAll, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Char1, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Char2, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Space, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
}


ExportRecordOfCharge::~ExportRecordOfCharge()
{
    delete ui;
}

void ExportRecordOfCharge::on_buttonOk_clicked()
{
    /*待完成*/
    QSqlDatabase db =  onInitAccount();
    if( ( !ui->m_editChargeRecordExport_CardNum->text().isEmpty() && !ui->m_editChargeRecordExport_StartTime->text().isEmpty() && !ui->m_editChargeRecordExport_EndTime->text().isEmpty()))
    {
        QRegExp regExp("(\\-)|(\\s)|(:)");
        QString card = reverse(ui->m_editChargeRecordExport_CardNum->text());
        QString start = ui->m_editChargeRecordExport_StartTime->text().replace(regExp, "");
        QString end = ui->m_editChargeRecordExport_EndTime->text().replace(regExp, "");
#if(PRODUCER != 3 && PRODUCER != 5)
        QString selectCmd = QString("select * from  tagAccount  where flag_finished != 85 and userId = '%1' and  starttime >= '%2' and  starttime <= '%3' order by starttime desc").arg(card).arg(start + QString("00000000")).arg(end + QString("23596000"));
#elif(PRODUCER == 3 || PRODUCER == 5)
        QString selectCmd = QString("select * from  tagAccount_xj  where flag_finished != 85 and userId = '%1' and  starttime >= '%2' and  starttime <= '%3' order by starttime desc").arg(card).arg(QString("20") + start + QString("000000")).arg(QString("20") + end + QString("235960"));
#endif
        qDebug() << "Cmd: " << selectCmd;
        accountModel->setQuery(selectCmd);
    }
    else if( ( !ui->m_editChargeRecordExport_CardNum->text().isEmpty() && !ui->m_editChargeRecordExport_StartTime->text().isEmpty() && ui->m_editChargeRecordExport_EndTime->text().isEmpty() ) )
    {
        QRegExp regExp("(\\-)|(\\s)|(:)");
        QString card = reverse(ui->m_editChargeRecordExport_CardNum->text());
        QString start = ui->m_editChargeRecordExport_StartTime->text().replace(regExp, "");
#if(PRODUCER != 3 && PRODUCER != 5)
        QString selectCmd = QString("select * from  tagAccount  where flag_finished != 85 and userId = '%1' and  starttime >= '%2'  order by starttime desc").arg(card).arg(start + QString("00000000"));
#elif(PRODUCER == 3 || PRODUCER == 5)
        QString selectCmd = QString("select * from  tagAccount_xj  where flag_finished != 85 and userId = '%1' and  starttime >= '%2'  order by starttime desc").arg(card).arg(QString("20") + start + QString("000000"));
#endif
        qDebug() << "Cmd: " << selectCmd;
        accountModel->setQuery(selectCmd);
    }
    else if(( !ui->m_editChargeRecordExport_CardNum->text().isEmpty() && ui->m_editChargeRecordExport_StartTime->text().isEmpty() && !ui->m_editChargeRecordExport_EndTime->text().isEmpty() ) )
    {
        QRegExp regExp("(\\-)|(\\s)|(:)");
        QString card = reverse(ui->m_editChargeRecordExport_CardNum->text());
        QString end = ui->m_editChargeRecordExport_EndTime->text().replace(regExp, "");
#if(PRODUCER != 3 && PRODUCER != 5)
        QString selectCmd = QString("select * from  tagAccount  where flag_finished != 85 and userId = '%1' and  starttime <= '%2' order by starttime desc").arg(card).arg(end + QString("23596000"));
#elif(PRODUCER == 3 || PRODUCER == 5)
        QString selectCmd = QString("select * from  tagAccount_xj  where flag_finished != 85 and userId = '%1' and  starttime <= '%2' order by starttime desc").arg(card).arg(QString("20") + end + QString("235960"));
#endif
        qDebug() << "Cmd: " << selectCmd;
        accountModel->setQuery(selectCmd);
    }
    else if( !ui->m_editChargeRecordExport_CardNum->text().isEmpty() && ( ui->m_editChargeRecordExport_StartTime->text().isEmpty() && ui->m_editChargeRecordExport_EndTime->text().isEmpty() ) )
    {
        QString card = reverse( ui->m_editChargeRecordExport_CardNum->text());
#if(PRODUCER != 3 && PRODUCER != 5)
        QString selectCmd = QString("select * from  tagAccount  where flag_finished != 85 and userId = '%1' order by starttime desc").arg(card);
#elif(PRODUCER == 3 || PRODUCER == 5)
        QString selectCmd = QString("select * from  tagAccount_xj  where flag_finished != 85 and userId = '%1' order by starttime desc").arg(card);
#endif
        qDebug() << "Cmd: " << selectCmd;
        accountModel->setQuery(selectCmd);
    }
    else if( ui->m_editChargeRecordExport_CardNum->text().isEmpty() && ( !ui->m_editChargeRecordExport_StartTime->text().isEmpty() && !ui->m_editChargeRecordExport_EndTime->text().isEmpty() ) )
    {
        QRegExp regExp("(\\-)|(\\s)|(:)");
        QString start = ui->m_editChargeRecordExport_StartTime->text().replace(regExp, "");
        QString end = ui->m_editChargeRecordExport_EndTime->text().replace(regExp, "");
#if(PRODUCER != 3 && PRODUCER != 5)
        QString selectCmd = QString("select * from  tagAccount  where flag_finished != 85 and starttime >= '%1' and  starttime <= '%2' order by starttime desc").arg(start + QString("00000000")).arg(end + QString("23596000"));
#elif(PRODUCER == 3 || PRODUCER == 5)
        QString selectCmd = QString("select * from  tagAccount_xj  where flag_finished != 85 and starttime >= '%1' and  starttime <= '%2' order by starttime desc").arg(QString("20") + start + QString("000000")).arg(QString("20") + end + QString("235960"));
#endif
        qDebug() << "Cmd: " << selectCmd;
        accountModel->setQuery(selectCmd);
    }
    //onAccountCuttentIndex(0);
    accountMapper->toFirst();
    QString fileName = readFile(QString("/usr/local/sanway/dbTest/charge.conf"), QString("devNO"));
    QString pathName = QString("/usr/local/sanway/dbTest/") + fileName + QString(".txt");
    onExprotFile(pathName, accountModel);
    db.close();
}

void ExportRecordOfCharge::on_buttonBack_clicked()
{
    stackLayout->setCurrentIndex(SystemSetting4Page);
}

void ExportRecordOfCharge::on_buttonExportAll_clicked()
{

    QSqlDatabase db =  onInitAccount();
#if (PRODUCER != 3 && PRODUCER != 5)
    accountModel->setQuery(QString("select * from tagAccount where flag_finished != 85 order by starttime desc"));
#elif(PRODUCER == 3 || PRODUCER == 5)
    accountModel->setQuery(QString("select * from tagAccount_xj where flag_finished != 85 order by starttime desc"));
#endif
    accountMapper->toFirst();
    QString fileName = readFile(QString("/usr/local/sanway/dbTest/charge.conf"), QString("devNO"));
    QString pathName = QString("/usr/local/sanway/dbTest/") + fileName + QString(".txt");
    onExprotFile(pathName, accountModel);
    db.close();

}
